﻿using NW_WEB_Service.App.Interface;
using Service.src.NW.Model;
using Service.src.NW.Model.Drive;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NW_WEB_Service.com.app.services
{
    public class DeviceService : DBService, DeviceInterface
    {

        private static object Lock = new object();

        public List<SysDevicrInfo> GetList()
        {
            List<SysDevicrInfo> list = new List<SysDevicrInfo>();

            list = DBInstance.QueryAll<SysDevicrInfo>();
            return list;
        }

        public List<SysPlcDriverAddress> GetAddresslist(int DriverId)
        {
            List<SysPlcDriverAddress> list = DBInstance.Query<SysPlcDriverAddress>(i => i.DriverId == DriverId);
            return list;
        }

        public SysDevicrInfo GetDeviceById(int Id)
        {
            SysDevicrInfo Info = DBInstance.Query<SysDevicrInfo>(i => i.Id == Id).FirstOrDefault();
            if (Info == null)
            {
                throw new Exception("获取设备异常，找不此设备");
            }
            return Info;
        }

        public SysDevicrInfo AddDrive(SysDevicrInfo DriveInfo)
        {
            DriveInfo = DBInstance.Install(DriveInfo);
            return DriveInfo;
        }

        public bool DeleteDriver( int id)
        {
            bool Res = DBInstance.Delete(new SysDevicrInfo() { Id = id });
            return Res;
        }

        /// <summary>
        /// 更新设备
        /// </summary>
        /// <param name="DriveInfo"></param>
        /// <returns></returns>
        public int UpldateDrive(SysDevicrInfo DriveInfo)
        {
            int i = DBInstance.Update(DriveInfo);
            new AppData().StartDevice(DriveInfo);
            return i;
        }
    }
}
